package com.lhl.mq.mqServer.datacenter;

import com.lhl.mq.MqApplication;
import com.lhl.mq.mqServer.core.Binding;
import com.lhl.mq.mqServer.core.Exchange;
import com.lhl.mq.mqServer.core.ExchangeType;
import com.lhl.mq.mqServer.core.MSGQueue;
import com.lhl.mq.mqServer.mapper.MetaMapper;

import java.io.File;
import java.util.List;

/**
 * 通过这个类来综合对数据库的操作
 */
public class DataBaseManager {

    //手动获取 MetaMapper
    private MetaMapper metaMapper;

    //对数据库进行初始化操作
    public void init(){
        metaMapper = MqApplication.context.getBean(MetaMapper.class);
        //判断是否已经建表
        if (!checkDBExists()){
            //创建目录
            File fileDir = new File("./data");
            fileDir.mkdirs();
            //创建表
            createTable();
            //插入默认数据
            createDefaultData();
            System.out.println("[dataBaseManager] 数据库初始化完成!");
        }else {
            //如果已近存在，则不用处理
            System.out.println("[dataBaseManager] 数据库已经存在!");
        }
    }

    public void deleteDB(){
        //删除数据库
        File file = new File("./data/meta.db");
        boolean result = file.delete();
        if (result){
            System.out.println("[dataBaseManager] 数据库删除成功！");
        }else {
            System.out.println("[dataBaseManager] 数据库删除失败！");
        }

        //删除目录
        File fileDir = new File("./data");
        boolean result1 = fileDir.delete();
        if (result1){
            System.out.println("[dataBaseManager] 目录删除成功！");
        }else {
            System.out.println("[dataBaseManager] 目录删除失败！");
        }

    }

    public boolean checkDBExists(){
        File file = new File("/data/meta.db");
        if (file.exists()){
            //数据库里面的表已经存在
            return true;
        }
        return false;
    }

    public void  createTable(){
        metaMapper.createBindingTable();;
        metaMapper.createQueueTable();;
        metaMapper.createExchangeTable();

        System.out.println("[dataBaseManager] 创建表完成");
    }

    //给数据库添加默认数据，主要是添加一个交换机
    //内部会一个默认的交换机，默认交换机类型为 DIRECT
    public void createDefaultData(){
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("[dataBaseManager] 创建初始数据完成");
    }



    //建关于数据库的其他方法
    public List<MSGQueue> selectQueue(){
       return metaMapper.selectAllQueue();
    }

    public void deleteQueue(String name){
        metaMapper.deleteQueue(name);
    }

    public void insertQueue(MSGQueue queue){
        metaMapper.insertQueue(queue);
    }

    public List<Exchange> selectExchange(){
        return metaMapper.selectAllExchange();
    }

    public void deleteExchange(String name){
        metaMapper.deleteExchange(name);
    }

    public void insertExchange(Exchange exchange){
        metaMapper.insertExchange(exchange);
    }

    public List<Binding> selectBinding(){
        return metaMapper.selectAllBinding();
    }

    public void deleteBinding(Binding binding){
        metaMapper.deleteBinding(binding);
    }

    public void insertBinding(Binding binding){
        metaMapper.insertBinding(binding);
    }
}
